package com.miku.blog.service.impl;

import com.miku.blog.domain.pojo.ResponseResult;
import com.miku.blog.domain.pojo.SystemConstance;
import com.miku.blog.eume.HttpStatusEnum;
import com.miku.blog.properties.QiNiuProperties;
import com.miku.blog.service.UploadFileService;
import com.miku.blog.utils.FilePathUtils;
import com.qiniu.common.QiniuException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author shkstart
 * @create ${}YEAR-05-29 17:18
 */

@Service
public class UploadFileServiceImpl implements UploadFileService {

    @Autowired
    private QiNiuProperties qiNiuProperties;


    @Override
    public ResponseResult uploadArticlesImages(MultipartFile multipartFile){

        String filename = multipartFile.getOriginalFilename();
        FilePathUtils.checkImageFileSuffixType(filename);
        String filePath
                = FilePathUtils.generateUploadFilePath(filename, SystemConstance.ARTICLE_COVER_FILEPATH_PREFIX);

        String urlPath;
        try {
            qiNiuProperties.getUploadManager().put(multipartFile.getBytes(), filePath, qiNiuProperties.getUploadToken());
//            DefaultPutRet defaultPutRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
            urlPath = qiNiuProperties.getUrl() + filePath;
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseResult(HttpStatusEnum.UPLOAD_FILE_ERROR);
        }


        return ResponseResult.ok().setData(urlPath);
    }

    @Override
    public ResponseResult deleteArticlesImages(String cover) {

        String key = cover.substring(qiNiuProperties.getUrl().length());
        try {
            qiNiuProperties.getBucketManager().delete(qiNiuProperties.getBucket(),key);
        } catch (QiniuException e) {
            e.printStackTrace();
            return new ResponseResult(HttpStatusEnum.UPDATE_DATA_ERROR);
        }

        return ResponseResult.ok();
    }


    @Override
    public ResponseResult uploadUserAvatarImages(MultipartFile multipartFile){

        String originalFilename = multipartFile.getOriginalFilename();
        FilePathUtils.checkImageFileSuffixType(originalFilename);

        String filePath
                = FilePathUtils.generateUploadFilePath(originalFilename, SystemConstance.USER_AVATAR_FILEPATH_PREFIX);

        String url;
        try {
            qiNiuProperties.getUploadManager().put(multipartFile.getBytes(),filePath,qiNiuProperties.getUploadToken());
            url = qiNiuProperties.getUrl() + filePath;
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseResult(HttpStatusEnum.UPLOAD_FILE_ERROR);
        }
        return ResponseResult.ok().setData(url);
    }

    @Override
    public ResponseResult deleteUserAvatarImages(String userAvatar) {

        String key = userAvatar.substring(qiNiuProperties.getUrl().length());

        try {
            qiNiuProperties.getBucketManager().delete(qiNiuProperties.getBucket(),key);
        } catch (QiniuException e) {
            e.printStackTrace();
            return new ResponseResult(HttpStatusEnum.UPDATE_DATA_ERROR);
        }

        return ResponseResult.ok();
    }


    @Override
    public ResponseResult uploadTalkImage(MultipartFile multipartFile) {

        String originalFilename = multipartFile.getOriginalFilename();
        FilePathUtils.checkImageFileSuffixType(originalFilename);
        String uploadFilePath
                = FilePathUtils.generateUploadFilePath(originalFilename, SystemConstance.TALK_IMAGE_FILEPATH_PREFIX);

        String path;
        try {
            qiNiuProperties.getUploadManager().put(multipartFile.getBytes(),uploadFilePath,qiNiuProperties.getUploadToken());
            path = qiNiuProperties.getUrl() + uploadFilePath;
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseResult(HttpStatusEnum.UPLOAD_FILE_ERROR);
        }
        return ResponseResult.ok().setData(path);
    }



    @Override
    public ResponseResult delTalkImage(String filePath){

        String key = filePath.substring(qiNiuProperties.getUrl().length());

        try {
            qiNiuProperties.getBucketManager().delete(qiNiuProperties.getBucket(),key);
        } catch (QiniuException e) {
            e.printStackTrace();
            return new ResponseResult(HttpStatusEnum.UPDATE_DATA_ERROR);
        }
        return ResponseResult.ok();
    }


    @Override
    public ResponseResult uploadPhotoAlbumCover(MultipartFile multipartFile) {

        String originalFilename = multipartFile.getOriginalFilename();
        FilePathUtils.checkImageFileSuffixType(originalFilename);
        String filePath = FilePathUtils.generateUploadFilePath(originalFilename,SystemConstance.PHOTO_ALBUM_COVER_FILEPATH_PREFIX);

        String path;
        try {
            qiNiuProperties.getUploadManager().put(multipartFile.getBytes(),filePath,qiNiuProperties.getUploadToken());
            path = qiNiuProperties.getUrl() + filePath;
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseResult(HttpStatusEnum.UPLOAD_FILE_ERROR);
        }
        return ResponseResult.ok().setData(path);
    }


    @Override
    public ResponseResult delPhotoAlbumCover(String filePath) {

        String key = filePath.substring(qiNiuProperties.getUrl().length());

        try {
            qiNiuProperties.getBucketManager().delete(qiNiuProperties.getBucket(),key);
        } catch (QiniuException e) {
            e.printStackTrace();
            return new ResponseResult(HttpStatusEnum.UPDATE_DATA_ERROR);
        }
        return ResponseResult.ok();
    }


    @Override
    public ResponseResult uploadWebsitePageCover(MultipartFile multipartFile) {
        String originalFilename = multipartFile.getOriginalFilename();
        FilePathUtils.checkImageFileSuffixType(originalFilename);
        String filePath
                = FilePathUtils.generateUploadFilePath(originalFilename, SystemConstance.WEBSITE_PAGE_COVER_FILEPATH_PREFIX);
        String path;
        try {
            qiNiuProperties.getUploadManager().put(multipartFile.getBytes(),filePath,qiNiuProperties.getUploadToken());
            path = qiNiuProperties.getUrl() + filePath;
        } catch (Exception exception) {
            exception.printStackTrace();
            return new ResponseResult(HttpStatusEnum.UPLOAD_FILE_ERROR);
        }
        return ResponseResult.ok().setData(path);
    }

    @Override
    public ResponseResult delWebsitePageCover(String filePath) {

        String key = filePath.substring(qiNiuProperties.getUrl().length());

        try {
            qiNiuProperties.getBucketManager().delete(qiNiuProperties.getBucket(),key);
        } catch (Exception exception) {
            exception.printStackTrace();
            return new ResponseResult(HttpStatusEnum.UPDATE_DATA_ERROR);
        }
        return ResponseResult.ok();
    }


    @Override
    public ResponseResult uploadWebsiteConfigImage(MultipartFile multipartFile) {

        String originalFilename = multipartFile.getOriginalFilename();
        FilePathUtils.checkImageFileSuffixType(originalFilename);
        String filePath = FilePathUtils.generateUploadFilePath(originalFilename, SystemConstance.WEBSITE_CONFIG_IMAGE_FILEPATH_PREFIX);

        String path;
        try {
            qiNiuProperties.getUploadManager().put(multipartFile.getBytes(),filePath,qiNiuProperties.getUploadToken());
            path = qiNiuProperties.getUrl() + filePath;
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseResult(HttpStatusEnum.UPLOAD_FILE_ERROR);
        }
        return ResponseResult.ok().setData(path);
    }

    @Override
    public ResponseResult delWebsiteConfigImage(String filePath) {

        String key = filePath.substring(qiNiuProperties.getUrl().length());

        try {
            qiNiuProperties.getBucketManager().delete(qiNiuProperties.getBucket(),key);
        } catch (QiniuException e) {
            e.printStackTrace();
            return new ResponseResult(HttpStatusEnum.UPDATE_DATA_ERROR);
        }
        return ResponseResult.ok();
    }
}
